# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=binutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.25.1
pkgrel=6
pkgdesc="A set of programs to assemble and manipulate binary and object files (mingw-w64)"
arch=('any')
url="http://www.gnu.org/software/binutils/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
#checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' '!distcc' '!ccache') # 'debug' '!strip')
#install=binutils.install
source=(https://ftp.gnu.org/gnu/binutils/${_realname}-${pkgver}.tar.bz2{,.sig}
        0001-enable-gold-on.mingw32.patch
        0002-check-for-unusual-file-harder.patch
        0003-enable-shared-bfd.all.patch
        0003-link-to-libibtl-and-libiberty.mingw.patch
        0004-libiberty-la.mingw.patch
        0005-shared-opcodes.mingw.patch
        0008-fix-libiberty-makefile.mingw.patch
        0009-fix-libiberty-configure.mingw.patch
        0013-dont-link-gas-to-libiberty.mingw.patch
        0014-dont-link-binutils-to-libiberty.mingw.patch
        0015-dont-link-ld-to-libiberty.mingw.patch
        0017-fix-iconv-linking.all.patch
        0110-binutils-mingw-gnu-print.patch
        27aaeda.diff
        a93d5cb.diff
        9ac47a4.diff)
sha256sums=('b5b14added7d78a8d1ca70b5cb75fef57ce2197264f4f5835326b0df22ac9f22'
            'SKIP'
            '93296b909e1a4f9d8a4bbe2437aafa17ca565ef6642a9812b0360c05be228c9d'
            'd182569814dbcad46ba6f2b4d72f4f827cefa7d0bd7030c89379613a53ca9786'
            'f084cf708da231584d1b1dd0fbdb6294ce176849c4f77d1666fa778df8d07781'
            '617adc1fd9fa66cc6e1da840b49edc1f88ae4bfd5b2807eeb34984eff8827e39'
            'b3fc8b894b9cfe9d0d27c49e36a808a0843020fccb3019737794fcde69af2352'
            'acae8564367601b4f601dca724596dbffbf056c0690e2aa090b185f3dcf2bc4f'
            '7c4349bbd4ad57157c896917fb692b41eb6d1ce50197c58fecbc45e5501b0104'
            '4849fe969fc7abb6b6dfcef4d4c6506f23831d86ba22c6dbf8d3039212654837'
            'fa009b8a2baf122700b9583fce47c0e304a37211af09c7dcd0a1169a511be362'
            '177426db0ac7f9e9794b1052cbd15c59fa0167ece4cb3ac4f72673403aabc97a'
            '376931c0c2f8f5d655458016ac496f93e75c00bf5538e445c29984920a2f5415'
            '6b336f218443a95e141d231e4fab726c85d27de7c60ad1ce9fb1a1687943a1ce'
            'bb20764800b394af3ffa12567158ae1cfcd4fb44077d4c44d4e261251e60cf9f'
            'ec32f5a63fe00315586d1502bd91fc0c700ce63174fc5eade3ae96420140b134'
            '2ed73d77ec8757cf0deef72aee44e55c0801ac3c75c79fdc727cf5fb7d9b983c'
            'de7f8850150921a321196822a18ddca8574e69e706323226c2156752318fb6d0')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-enable-gold-on.mingw32.patch
  patch -p1 -i "${srcdir}"/0002-check-for-unusual-file-harder.patch
  #patch -p1 -i "${srcdir}"/0003-enable-shared-bfd.all.patch
  #patch -p1 -i "${srcdir}"/0003-link-to-libibtl-and-libiberty.mingw.patch
  #patch -p1 -i "${srcdir}"/0004-libiberty-la.mingw.patch
  #patch -p1 -i "${srcdir}"/0005-shared-opcodes.mingw.patch
  patch -p1 -i "${srcdir}"/0008-fix-libiberty-makefile.mingw.patch
  patch -p1 -i "${srcdir}"/0009-fix-libiberty-configure.mingw.patch
  #patch -p1 -i "${srcdir}"/0013-dont-link-gas-to-libiberty.mingw.patch
  #patch -p1 -i "${srcdir}"/0014-dont-link-binutils-to-libiberty.mingw.patch
  #patch -p1 -i "${srcdir}"/0015-dont-link-ld-to-libiberty.mingw.patch
  #patch -p1 -i "${srcdir}"/0017-fix-iconv-linking.all.patch
  patch -p1 -i "${srcdir}"/0110-binutils-mingw-gnu-print.patch
  
  # Upstream patches
  patch -p1 -i "${srcdir}"/27aaeda.diff
  patch -p1 -i "${srcdir}"/a93d5cb.diff
  patch -p1 -i "${srcdir}"/9ac47a4.diff

}

build() {
  if [ "${CARCH}" = "x86_64" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-werror \
    --enable-lto \
    --with-sysroot=${MINGW_PREFIX} \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    $_conf \
    --enable-nls \
    --disable-rpath \
    --disable-multilib \
    --enable-install-libiberty \
    --enable-plugins \
    --enable-gold \
    --disable-shared

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{lib,include}/${_realname}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include
  cp ${pkgdir}${MINGW_PREFIX}/include/ansidecl.h ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/
  mv ${pkgdir}${MINGW_PREFIX}/include/*.h ${pkgdir}${MINGW_PREFIX}/include/${_realname}
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.a ${pkgdir}${MINGW_PREFIX}/lib/${_realname}

  # Add some useful headers
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/libiberty.h ${pkgdir}${MINGW_PREFIX}/include
  #install -m644 ${srcdir}/${_realname}-${pkgver}/include/demangle.h ${pkgdir}${MINGW_PREFIX}/include

  find  ${pkgdir}${MINGW_PREFIX}/share -type f -iname "opcodes.mo" -o -iname "bfd.mo" | xargs -rtl1 rm
}
